#include "front/text_normalize.h"

using namespace ppspeech;

string TextNormalizer::TnZh_EachDigit2Str(const string &fcInputNumStr, const bool fcInUseAllYao1)
{
	const vector<string>* tpVecNumber = fcInUseAllYao1 ? &mcVecNumberYao1 : &mcVecNumberYi1;
	string tResultStr = "";
	for(const char tChar : fcInputNumStr) {
		if(tChar >= '0' && tChar <= '9') {		//数字项
			tResultStr += tpVecNumber->at( tChar-'0' );
		}
		else if(tChar >= 'a' && tChar <= 'z') {	//小写字母项
			tResultStr += char(tChar-0x20);
			tResultStr += " ";
		}
		else {									//其他项
			tResultStr += tChar;
			tResultStr += " ";
		}
	}
	return tResultStr;
}


string TextNormalizer::TnZh_NumNegDec2Int(const string &fcInputNumStr,
				const bool fcInUseAll1Yi1, const bool fcInUseAll1Yao1,
				const bool fcInUseOnly2Liang3, const bool fcInUseAll2Liang3)
{
	const bool tcInUseZero = false;
	if(fcInputNumStr.length() <= 0) {
		return "";
	}

	string tInputStr = fcInputNumStr;
	string tResultStr = "";

	//-----------------
	if(tInputStr[0] == '-') {				//带负号
		tResultStr += "负";
		tInputStr = tInputStr.substr(1);	//去掉负号
	}
	else if(tInputStr[0] == '+') {			//带正号
		tResultStr += "正";
		tInputStr = tInputStr.substr(1);	//去掉正号
	}

	//-----------------
	vector<string> tItemDot = StrSplit(tInputStr, ".");
	//整数
	if(tItemDot[0].length() > 0) {
		if(stoi(tItemDot[0]) == 1 && fcInUseAll1Yi1 == true) {
			tResultStr += "壹";
		}
		else {
			tResultStr += TnZh_Digit2Int(tItemDot[0], tcInUseZero,
				fcInUseAll1Yi1, fcInUseAll1Yao1, fcInUseOnly2Liang3, fcInUseAll2Liang3);
		}
	}
	
	if(tItemDot.size() > 1) {	//带小数
		tResultStr += "点";
		tResultStr += TnZh_EachDigit2Str(tItemDot[1], false);
	}

	return tResultStr;
}



string TextNormalizer::TnZh_Digit2Int(const string &fcInputNumStr, const bool fcInUseZero,
										const bool fcInUseAll1Yi1, const bool fcInUseAll1Yao1,
										const bool fcInUseOnly2Liang3, const bool fcInUseAll2Liang3)
{
	wstring result = utf8string2wstring(TnZh_Num2Chinese(fcInputNumStr, fcInUseZero));

	if(fcInUseAll1Yao1) {			//强制读"一"读"yao1"
		WstrReplace(result, "一", "幺");
	}
	else if(fcInUseAll1Yi1) {		//"一"读"yi1"
		if(fcInputNumStr == "1") {
			return "壹";
		}
		else {
			WstrReplace(result, "万一", "万壹");
			WstrReplace(result, "千一", "千壹");
			WstrReplace(result, "十一", "十壹");
			WstrReplace(result, "一十", "壹十");
			WstrReplace(result, "零一", "零壹");
		}
	}

	if(fcInUseOnly2Liang3) {		//"数字2"读"liang3"
		if(fcInputNumStr == "2") {
			WstrReplace(result, "二", "两");
		}
	}
	if(fcInUseAll2Liang3) {			//"2"读"liang3"
		WstrReplace(result, "二亿", "两亿");
		WstrReplace(result, "二万", "两万");
		WstrReplace(result, "二千", "两千");
		WstrReplace(result, "二百", "两百");

		WstrReplace(result, "十两亿", "十二亿");
		WstrReplace(result, "十两万", "十二万");
	}

	return wstring2utf8string(result);
}


//-----------------------------------------------------------------------------------------
const string mcChineseNumberChar[10] = {"零","一","二","三","四","五","六","七","八","九"};
const string mcChineseUnitSection[4] = {"", "万", "亿", "万亿"};
const string mcChineseUnitChar[4] = {"", "十","百","千"};
// 数字转文本，10200 - > 一万零二百
string TextNormalizer::TnZh_Num2Chinese(const string &fcInputNumStr, const bool fcInUseZero)
{
	for(const char tcNumChar : fcInputNumStr) {
		if(tcNumChar < '0' || tcNumChar > '9') {	//不是纯粹的数字则退出
			return "";
		}
	}

	//中文数字字符串转为数值
	long long tLongNum = stol(fcInputNumStr);
	if(0 == tLongNum) {
		return mcChineseNumberChar[0];
	}

	string tChineseStr = "";
	string tStrInsert = "";
	int tUnitPos = 0;
	bool tNeedZero = false;

	while(tLongNum > 0)
	{
		unsigned int section = tLongNum % 10000;
		if(tNeedZero) {
			tChineseStr.insert(0, mcChineseNumberChar[0]);
		}
		tStrInsert = TnZh_Section2Chinese(section, fcInUseZero);
		//是否需要节权位？
		tStrInsert += (section != 0) ? mcChineseUnitSection[tUnitPos] : mcChineseUnitSection[0];
		tChineseStr.insert(0, tStrInsert);
		tStrInsert = "";
		//千位是0需要在下一个section补零
		tNeedZero = (section < 1000) && (section > 0);
		tLongNum /= 10000;
		tUnitPos++;
	}
	return tChineseStr;
}


string TextNormalizer::TnZh_Section2Chinese(const unsigned int fcInSection, const bool fcInUseZero)
{
	string tChineseStr = "";
	string tStrInsert = "";
	int tUnitPos = 0;
	bool tZeroFlag = true;

	unsigned int section = fcInSection;
	while(section > 0)
	{
		int v = section % 10;
		if(v == 0)
		{
			if( (section ==0) || (tZeroFlag==false) )
			{
				tZeroFlag = true;        //需要补，zero的作用是确保对连续的多个，只补一个中文零
				if(fcInUseZero == false)
				{
					tChineseStr.insert(0, mcChineseNumberChar[v]);
				}
			}
		}
		else
		{
			tZeroFlag = false;                              //至少有一个数字不是
			tStrInsert = mcChineseNumberChar[v];            //此位对应的中文数字
			if (("一" == mcChineseNumberChar[v]) and ("十" == mcChineseUnitChar[tUnitPos]))
			{
				tStrInsert = mcChineseUnitChar[tUnitPos];
				if(section / 10 > 0)
				{
					tStrInsert = "一" + tStrInsert;
				}
			}
			else
			{
				tStrInsert += mcChineseUnitChar[tUnitPos];  //此位对应的中文权位
			}
			tChineseStr.insert(0, tStrInsert);
		}
		tUnitPos++;         //移位
		section /= 10;
	}

	return tChineseStr;
}
